﻿// -----------------------------------------------------------------------
// <copyright file="Repository.cs" company="Hewlett-Packard">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Ex3.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Xml.Serialization;
    using FacebookWrapper.ObjectModel;

    /// <summary>
    /// Abstract class for xml repository
    /// </summary>
    public abstract class XmlRepository
    {
        public User LoggedinUser { get; protected set; }

        public XmlRepository(User i_LoggedinUser)
        {
            this.LoggedinUser = i_LoggedinUser;
        }

        protected void saveFile(object i_Object, string fullPath)
        {
            using (FileStream stream = File.Create(fullPath))
            {
                XmlSerializer serializer = new XmlSerializer(i_Object.GetType());
                serializer.Serialize(stream, i_Object);
            }
        }

        protected void verifyFileDirectoryExists(string i_Directory)
        {
            bool isExists = Directory.Exists(i_Directory);

            if (!isExists)
            {
                Directory.CreateDirectory(i_Directory);
            }
        }

        protected string getFileDirectory()
        {
            string directoryPathOfXML = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), this.LoggedinUser.Id);
            return directoryPathOfXML;
        }
    }
}
